
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import java.util.StringTokenizer;



public class TCPDesktopServer implements Runnable{

    public static final String SERVERIP = "127.0.0.1";
    public static final int SERVERPORT = 1111;

    public void run() {
         try {
        	 
        	 InetAddress inetAddress = InetAddress.getLocalHost();
        	 System.out.println("S: Connecting..."+ inetAddress.toString());
             ServerSocket serverSocket = new ServerSocket(SERVERPORT);
             while (true) {      	 
            	 Socket client = serverSocket.accept();
            	 System.out.println("S: Receiving...");

            	 try {

                      BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                      String str = in.readLine();
                      System.out.println("S: Received: '" + str + "'");
                      processMessage(str);
                    
            	 } catch(Exception e) {
                        System.out.println("S: Error");
                        e.printStackTrace();
                    
            	 } finally {
                    	client.close();
                        System.out.println("S: Done.");
                    }
             }       
         } catch (Exception e) {
             System.out.println("S: Error");
             e.printStackTrace();
         }
    }
    
    public void processMessage(String message){
    	StringTokenizer parser = new StringTokenizer(message, " ");
    	int command=Integer.parseInt(parser.nextToken());
    	if(command == 0){
    		
    	}
    	System.out.println("S: Command is:" + command + " and the rest of the message is: " + message);
    }

    public void authenticationLogin(String message){
    	
    }
    
    public static void main (String a[]) {

    	Thread desktopServerThread = new Thread(new TCPDesktopServer());
    	desktopServerThread.start();
    }
}
